Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
60 lines · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { APIRoute } from "astro";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { getDatabase, schema } from "@/db";
import { forbidden, notFound, parseBody, success, unauthorized } from "@/lib/api";
import { withErrorHandler } from "@/lib/errors";
import { getUserFromRequest } from "@/lib/auth";
import { getChangeSetWithItems } from "@/lib/dependency-awareness";

const patchSchema = z.object({
  status: z.enum(["draft", "ready", "merged", "abandoned"]).optional(),
  name: z.string().min(1).max(120).optional(),
  description: z.string().max(600).nullable().optional(),
});

export const GET: APIRoute = withErrorHandler(async ({ params, request }) => {
  const user = await getUserFromRequest(request);
  if (!user) return unauthorized();

  const id = params.id;
  if (!id) return notFound("Change set not found");
  const payload = await getChangeSetWithItems(id);
  if (!payload) return notFound("Change set not found");
  if (payload.changeSet.createdById !== user.userId && !user.isAdmin) return forbidden();
  return success(payload);
});

export const PATCH: APIRoute = withErrorHandler(async ({ params, request }) => {
  const user = await getUserFromRequest(request);
  if (!user) return unauthorized();
  const id = params.id;
  if (!id) return notFound("Change set not found");

  const db = getDatabase() as NodePgDatabase<typeof schema>;
  const existing = await db.query.changeSets?.findFirst({
    where: eq(schema.changeSets.id, id),
  });
  if (!existing) return notFound("Change set not found");
  if (existing.createdById !== user.userId && !user.isAdmin) return forbidden();

  const parsed = await parseBody(request, patchSchema);
  if ("error" in parsed) return parsed.error;

  await db
    .update(schema.changeSets)
    .set({
      ...(parsed.data.status ? { status: parsed.data.status } : {}),
      ...(parsed.data.name ? { name: parsed.data.name } : {}),
      ...(parsed.data.description !== undefined ? { description: parsed.data.description } : {}),
      updatedAt: new Date(),
    })
    .where(eq(schema.changeSets.id, id));

  const updated = await db.query.changeSets?.findFirst({
    where: eq(schema.changeSets.id, id),
  });
  return success(updated);
});